/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.adhocracy.cli;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.isNull;
import static com.google.common.base.Predicates.notNull;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

/**
 *
 * @author tim
 */
public class CommandLineReactorTransactionCallback extends TransactionCallbackWithoutResult {

    private CommandLineReactor reactor;

    public void setCommandLineReactor(CommandLineReactor r) {
        checkState(isNull().apply(reactor), "reactor property already set");
        this.reactor = r;
    }

    public void verifyState() {
        checkState(notNull().apply(reactor), "reactor property not set");
    }

    protected void doInTransactionWithoutResult(TransactionStatus status) {
        try {
            reactor.init();
            reactor.process();
            reactor.destroy();
        } catch (RuntimeException ex) {
            status.setRollbackOnly();
            throw ex;
        }
    }
}
